{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 利用 RASA 构建自己的天气聊天机器人\n",
    "\n",
    "在这个项目中，我们将把 RASA 解析出来的 Intent 传递给 custom action server, custom action server 在接受到 Intent 中，通过调用 天气API 来查询天气，并返回给客户端。\n",
    "\n",
    "在开始之前，需要先运行 custom actions server\n",
    "\n",
    "```shell\n",
    "rasa run actions\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 设置 Jupyter Notebook\n",
    "\n",
    "设置 Jupyter Notebook 的输出，只打印有用的信息，忽略没用的 warning 信息。\n",
    "\n",
    "如果使用 Jupyter Notebook, 请查看[Using Jupyter Notebook](https://rasa.com/docs/rasa/api/jupyter-notebooks/)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import nest_asyncio\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Logging settings\n",
    "import logging, io, json, warnings, pprint\n",
    "logging.basicConfig(level=\"ERROR\")\n",
    "warnings.filterwarnings('ignore')\n",
    "pp = pprint.PrettyPrinter(indent=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RASA 项目设置\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = \"config.yml\"\n",
    "training_files = \"data_exp3/\"\n",
    "domain = \"domain.yml\"\n",
    "output = \"models/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 训练数据\n",
    "我们已经将一些训练数据，提前放到了 [data/](data/)目录下。现在我们先预览一下这些数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pycat data_exp3/nlu.md"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pycat data_exp3/stories.md"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 开始进行训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import rasa\n",
    "model_path = rasa.train(domain, config, [training_files], output)\n",
    "print(model_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 尝试和 chatbot 进行对话\n",
    "\n",
    "`chat()` 这个方法无法定制 `endpoints.yml`, 所以我们使用 `rasa` 命令行在 terminal 中进行测试：\n",
    "\n",
    "```shell\n",
    "rasa shell --endpoints endpoints.yml\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实验结束。Have a nice day."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
